<script setup lang='ts'>
  import { onActivated, reactive, ref } from 'vue';
  import { gamesApi } from '@/api/controller';
  import { taskSendType } from '@/pinia/getter';
  import { ToastSuccess, ToastWarning, Toastloading } from '@/utils/public';
  import Pagination from "@/components/Pagination/index.vue";
  import signinForm from './signinForm.vue';
  import { ElMessageBox } from 'element-plus';
  import xmImage from '@/components/xmImage/index.vue';
  import { useRoute } from 'vue-router';
  const $route = useRoute()
  const signinFormRef = ref()
  const action = reactive({
    total:0,
    list:[] as any[],
  })
  const listQuery = reactive({
    id:'' as string | number,
    page:1,
    limit:20,
  })
  const handleSerach = ()=>{
    if(!listQuery.id) return ToastWarning('缺少活动配置ID')
    listQuery.page = 1
    getList()
  }
  const handleForm = (row:any = {})=>{
    if(!listQuery.id) return ToastWarning('缺少活动配置ID')
    signinFormRef.value.openDialog({
      ...row,
      activityID:listQuery.id,
    })
  }
  const handleDelete = (row:any = {})=>{
    const { sigin_id=0, award_name='' } = row
    if(!sigin_id) return ToastWarning('缺少sigin_id')
    ElMessageBox.confirm(`确定删除签到配置：${award_name}吗?`, "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(async () => {
      const loading = Toastloading();
      gamesApi.del_sigin_config({sigin_id}).then(()=>{
        ToastSuccess("删除成功!");
        loading.close();
        getList();
      }).catch(()=>{
        loading.close();
      })
    }).catch(() => {});
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await gamesApi.sigin_list(listQuery)
    loading.close()
    const { data=[], total=0 } = res.data
    action.total = total
    action.list = data
  }
  onActivated(()=>{
    const { activityID='' } = $route.query as {activityID:string}
    if(activityID) {
      listQuery.id = activityID
      getList()
    }
  })
</script>

<template>
  <div class="xm-content">
    <div class="xm-headTab">
      <div class="labelBox">
        <div class="labelItem">
          <div class="label">活动ID：</div>
          <el-input type="text" v-model="listQuery.id" placeholder="请输入活动ID" />
        </div>
        <div class="labelItem">
          <el-button type="primary" @click="handleSerach">查询</el-button>
          <el-button type="primary" @click="handleForm">创建签到奖励</el-button>
        </div>
      </div>
    </div>
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border
    >
      <el-table-column width="120" prop="day" label="天数" />
      <el-table-column min-width="160" label="奖励类型">
       <template #default="scoped">
         {{ taskSendType.find(res=>res.type == scoped.row.send_type)?.label || scoped.row.send_type }}
       </template>
      </el-table-column>
      <el-table-column width="160" prop="goods_id" label="奖励ID" />
      <el-table-column min-width="180" prop="award_name" label="奖励名称" />
      <el-table-column min-width="100" prop="send_num" label="奖励数量" />
      <el-table-column width="140" label="奖励图片">
        <template #default="scoped">
          <xmImage :src="scoped.row.img" :list="[scoped.row.img]" />
        </template>
      </el-table-column>
      <el-table-column width="180" fixed="right" label="操作">
        <template #default="scoped">
          <el-button type="primary" @click="handleForm(scoped.row)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scoped.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="action.total > 0" :total="action.total" v-model:page="listQuery.page"
      v-model:limit="listQuery.limit" @pagination="getList" />
    <signinForm ref="signinFormRef" @update-list="getList" />
  </div>
</template>